<template>
	<view>
		<view class="text-center bg-white">
			<image class="cu-avatar round xl margin-top bg-white" :src="company?.logo" mode=""></image>
			<view class="margin text-lg text-bold">
				{{company?.companyName}}
			</view>
			<view class="margin">
				{{company?.industry}}
			</view>
			<view class="">
				{{company?.workLocation}}
			</view>
		</view>
		<custom-tab v-model="actIdx"></custom-tab>
		<view class="" v-if="actIdx==0">
			<view class="padding bg-white">
				<view class="text-bold margin-bottom-sm">
					公司简介
				</view>
				<view class="text-gray">
					{{company?.companyIntroduction}}
				</view>
			</view>
			<view class="padding bg-white margin-top-sm">
				<view class="text-bold margin-bottom-sm">
					公司地址
				</view>
				<view class="text-gray">
					{{company?.address}}
				</view>
			</view>
			<view class="padding bg-white">
				<view class="text-bold margin-bottom-sm">
					公司法人
				</view>
				<view class="text-gray">
					{{company?.legalRepresentative}}
				</view>
			</view>
			<view class="padding bg-white">
				<view class="text-bold margin-bottom-sm">
					公司福利
				</view>
				<view class="text-gray">
					{{company?.welfare}}
				</view>
			</view>
			<view class="padding bg-white">
				<view class="text-bold margin-bottom-sm">
					公司规模
				</view>
				<view class="text-gray">
					{{company?.companySizeDetail}}
				</view>
			</view>
		</view>
		<view class="bg-white padding" v-if="actIdx==1">
			<company-job v-for="item in job" 
			:key= "item.positionId || item"
			:data="item"
			@click="handleJobClick(item)"
			:class="{ 'selected': selectedJobs.includes(item.positionId) }"
			/>
		</view>
		<view class="cu-bar foot bg-white " v-if="actIdx==1">
			<button class="cu-btn bg-orange"@click="handleJobBatch">
				岗位关联
			</button>
		</view>
	</view>
</template>

<script setup lang="ts">
import { onLoad } from '@dcloudio/uni-app';
import {http} from '../../utils/http'
import { ref } from 'vue';
import type{ CompanyType } from '../../model/company';
import type{ Ref } from 'vue';
import job from '../../utils/job_dataset'
import type{JobType} from '../../model/job'
//通过路由接受岗位id，发起网络请求，渲染企业信息
const company:Ref<CompanyType|null> = ref(null)
const actIdx = ref<number>(0)
const selectedJobs = ref<any[]>([]);
// 多选
const handleJobClick = (item:JobType) => {
  const index = selectedJobs.value.indexOf(item.positionId);
  if (index > -1) {
    selectedJobs.value.splice(index, 1);
  } else {
    selectedJobs.value.push(item.positionId);
  }
};
const handleJobBatch = ()=>{
	let checkList = job.filter((item)=>selectedJobs.value.includes(item.positionId))
	let arr = checkList.map((item)=>{
		return{
			method:'POST',
			path:'/1.1/classes/Job',
			body:{
				...item,
				companyName:company.value?.companyName,//冗余字段 提高后续查询效率
				logo:company.value?.logo,
				companyId:company.value?.objectId
			}
		}
	})
	console.log('arr',arr);
	http.post('/batch',{
		requests:arr
	}).then((res)=>{
		uni.showToast({
			title:'关联成功',
			icon:'success'
		})
		selectedJobs.value = []

	})
}

onLoad(async(options)=>{
	let companyId = options?.id ||'68932c7190e1ae583be38f28'
	// 方法一:
	// let res = await http.get('/classes/Company',{params:{where:{'objectId':companyId}}})
	// console.log('详情数据',res);
	// 方法二:
	let res = await http.get(`classes/Company/${companyId}`)
	console.log('详情数据',res);
	company.value = res.data
})
</script>

<style lang="scss">
.selected {
  	// 这里可以自定义选中样式，例如添加背景色
	//background-color: aqua !important;
	border: 1upx solid orange !important;
}
.cu-bar.foot button {
	width: 90%;
	margin-left: 5%;
}
</style>
